<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
      </template>
      <template #orderNo="{ record }">
        <div class="order-list">
          <p><span style="color: #729ED5; background: #e7f5f7;">支付</span>{{ record.orderNo }}</p>
          <p style="margin-bottom: 0">
            <span style="color: #56cf56; background: #d8eadf;">商户</span>
            <Tooltip placement="bottom" style="font-weight: normal;"
                     v-if="record.outTradeNo.length > record.orderNo.length">
              <template slot="title">
                <span>{{ record.outTradeNo }}</span>
              </template>
              {{ changeStr2ellipsis(record.mchOrderNo, record.payOrderId.length) }}
            </Tooltip>
            <span style="font-weight: normal;" v-else>{{ record.outTradeNo }}</span>
          </p>
          <p v-if="record.channelOrderNo" style="margin-bottom: 0; margin-top: 10px">
            <span style="color: #fff; background: #E09C4D;">渠道</span>
            <Tooltip placement="bottom" style="font-weight: normal;"
                     v-if="record.channelOrderNo.length > record.orderNo.length">
              <template slot="title">
                <span>{{ record.channelOrderNo }}</span>
              </template>
              {{ changeStr2ellipsis(record.channelOrderNo, record.orderNo.length) }}
            </Tooltip>
            <span style="font-weight: normal;" v-else>{{ record.channelOrderNo }}</span>
          </p>
        </div>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:search-outlined',
              tooltip: '查看订单详情',
              onClick: handleView.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <OrderDetailDrawer @register="registerDrawer"/>
  </div>
</template>
<script lang="ts">
  import {defineComponent} from 'vue';
  import {Tooltip} from 'ant-design-vue';

  import {BasicTable, useTable, TableAction} from '/@/components/Table';

  import {useDrawer} from '/@/components/Drawer';
  import PayOrderDrawer from './PayOrderDrawer.vue';
  import OrderDetailDrawer from './OrderDetailDrawer.vue';

  import {columns, searchFormSchema} from './pay.data';
  import {pageList} from './pay.api';

  export default defineComponent({
    name: 'PayOrderManagement',
    components: {BasicTable, TableAction, PayOrderDrawer, OrderDetailDrawer, Tooltip},
    setup() {
      const [registerTable, {reload}] = useTable({
        title: '支付订单列表',
        api: pageList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: {customRender: 'action'},
          fixed: undefined,
        },
      });
      const [registerDrawer, {openDrawer}] = useDrawer();

      function handleView(record: Recordable) {
        openDrawer(true, {record});
      }

      function changeStr2ellipsis(orderNo, baseLength) {
        const halfLength = parseInt(baseLength / 2)
        return orderNo.substring(0, halfLength - 1) + '...' + orderNo.substring(orderNo.length - halfLength, orderNo.length)
      }

      return {
        registerTable,
        reload,
        registerDrawer,
        changeStr2ellipsis,
        handleView,
      };
    },
  });
</script>

<style lang="less" scoped>
  .order-list {
    -webkit-text-size-adjust: none;
    font-size: 12px;
    display: flex;
    flex-direction: column;

    p {
      white-space: nowrap;

      span {
        display: inline-block;
        font-weight: 800;
        height: 16px;
        line-height: 16px;
        /*width: 35px;*/
        border-radius: 5px;
        text-align: center;
        margin-right: 2px;
      }
    }
  }
</style>
